package org.jeecg.modules.cw.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.CwJbsrysTj;
import org.jeecg.modules.cw.entity.CwJbsrys;
import org.jeecg.modules.cw.mapper.CwJbsrysMapper;
import org.jeecg.modules.cw.mapper.CwJbsrysTjMapper;
import org.jeecg.modules.cw.service.ICwJbsrysTjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 基本收入预算明细提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-15
 * @Version: V1.0
 */
@Service
public class CwJbsrysTjServiceImpl extends ServiceImpl<CwJbsrysTjMapper, CwJbsrysTj> implements ICwJbsrysTjService {

	@Autowired
	private CwJbsrysTjMapper cwJbsrysTjMapper;
	@Autowired
	private CwJbsrysMapper cwJbsrysMapper;
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwJbsrysTj cwJbsrysTj, List<CwJbsrys> cwJbsrysList) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		cwJbsrysTj.setUnit(departName);
		cwJbsrysTj.setMainId(user.getId());
		cwJbsrysTj.setState("待提交");
		cwJbsrysTjMapper.insert(cwJbsrysTj);


		if(cwJbsrysList!=null && cwJbsrysList.size()>0) {
			for(CwJbsrys entity:cwJbsrysList) {
				entity.setUnit(cwJbsrysTj.getUnit());
				entity.setYear(cwJbsrysTj.getYear());
				entity.setUserId(user.getId());
				entity.setCe(entity.getCwSj()-entity.getCwYs());
				//外键设置
				entity.setMianId(cwJbsrysTj.getId());
				cwJbsrysMapper.insert(entity);
			}
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwJbsrysTj cwJbsrysTj,List<CwJbsrys> cwJbsrysList) {
		cwJbsrysTjMapper.updateById(cwJbsrysTj);
		
		//1.先删除子表数据
		cwJbsrysMapper.deleteByMainId(cwJbsrysTj.getId());
		
		//2.子表数据重新插入
		if(cwJbsrysList!=null && cwJbsrysList.size()>0) {
			for(CwJbsrys entity:cwJbsrysList) {
				//外键设置
				entity.setMianId(cwJbsrysTj.getId());
				entity.setCe(entity.getCwSj()-entity.getCwYs());
				cwJbsrysMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwJbsrysMapper.deleteByMainId(id);
		cwJbsrysTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwJbsrysMapper.deleteByMainId(id.toString());
			cwJbsrysTjMapper.deleteById(id);
		}
	}
	
}
